package com.qf.shoppingorder.controller;

import com.qf.shoppingorder.entity.Order;
import com.qf.shoppingorder.mapper.OrderMapper;
import com.qf.shoppingorder.util.CacheManagerSevice;
import entity.ResultData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class RabbitController {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMDDHHmmss");
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    CacheManagerSevice cacheManagerSevice;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @ResponseBody
    @GetMapping("/buy")
    public String buy(Order order) {
//        Order order = new Order();
        for (int i = 0; i < 1; i++) {
            order.setId(i);
            order.setStatus("-1");
            //发送消息
            rabbitTemplate.convertAndSend("android-exchange", "android-route", order);
//            rabbitTemplate.convertAndSend("fanout-exchange");
        }

        return "ok";
    }

    @ResponseBody
    @GetMapping("/mqsend")
    public String mqsend(String msg) {
        System.out.println("mqsend");
//        Order order = new Order();
        //发送消息
        rabbitTemplate.convertAndSend("android-exchange", "android-route", msg);
//            rabbitTemplate.convertAndSend("fanout-exchange");
        return "ok";
    }

    @GetMapping("/pay")
    @ResponseBody
    public ResultData pay(Integer id, String orderNumber) {
//        Order order = new Order();
//        order.setId(11);
//        order.setStatus("-1");
        //发送消息
//        rabbitTemplate.convertAndSend("delay-exchange", "delay-route", order);
//        return "ok";
        Order order = new Order();
        order.setId(id);
        order.setStatus("1");
        order.setPayTime(sdf.format(new Date()));
        int i = orderMapper.updateById(order);
        cacheManagerSevice.del("order_" + orderNumber);
        if (i > 0) {
            return new ResultData(true, "支付成功");
        }
        return new ResultData(false, "支付失败");
    }

//    @RabbitListener(queues = {"release.queue"})
////    todo 监听消息 直接打印对象
////    消费者确认
//    public void recv(Message message, Order order, Channel channel) throws IOException {
////        byte[] order = message.getBody();//二进制
//        System.out.println("release.queue===");
//        System.out.println(message);
//        System.out.println(order);
//        Order o = (Order)cacheManagerSevice.get(order.getOrderNumber());
//        orderMapper.deleteById(o.getId());
//        //未支付
////        if (o != null) {
////            //删除订单
////            orderMapper.deleteById(o.getId());
////        } else {
////
////        }
//        // TODO: 2024/3/19 消费者确认 com.rabbitmq.client.Channel
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
////        deliveryTag为消息标签 第二个参数:是否开启批量确认
//        channel.basicAck(deliveryTag, false);
//
//    }

    /**
     * todo @RabbitListener 和 @RabbitHandler 搭配使用
     * RabbitListener(队列名)用在类上，
     * RabbitHandler用在方法上，根据收到的消息类型决定该消息由哪个RabbitHandler处理
     * 举例 Order类型第一个RabbitHandler消费 Product类型第一个RabbitHandler消费
     *
     * @param message
     * @param order
     * @throws IOException
     * @RabbitHandler public void recv(Message message, Order order)
     * @RabbitHandler public void recv(Message message, Product product)
     */
//    @RabbitListener(queues = {"android.queue"})
//    public void recv2(Message message, Order order) throws IOException {
//        System.out.println("android.queue===");
//        System.out.println(message);
//    }


//    @RabbitListener(queues = {"delay.queue"})
//    public void recv2(Message message) {
//        System.out.println(message);
//    }
}
